package engine

import (
	"fmt"
	"hitme/go-api/modules/config"
	"sync"
)

type Engine struct {
	config       *config.Config
	announceLock sync.Once
}

func Default() *Engine {
	return &Engine{}
}

func (eng *Engine) announce() *Engine {
	if eng.config.Debug {
		eng.announceLock.Do(func() {
			fmt.Printf("go-api is now running. \nrunning in \"debug\" mode. switch to \"release\" mode in production.\n\n")
		})
	}
	return eng
}

// AddConfig 添加全局配置
func (eng *Engine) AddConfig(cfg *config.Config) *Engine {
	return eng.setConfig(cfg).announce()
}

// setConfig 设置配置文件信息
func (eng *Engine) setConfig(cfg *config.Config) *Engine {
	eng.config = config.Initialize(cfg)
	return eng
}

// AddConfigFromJSON 添加json格式配置文件
func (eng *Engine) AddConfigFromJSON(path string) *Engine {
	cfg := config.ReadFromJson(path)
	return eng.setConfig(&cfg).announce()
}

// AddConfigFromYAML 添加YAML格式配置文件
func (eng *Engine) AddConfigFromYAML(path string) *Engine {
	cfg := config.ReadFromYaml(path)
	return eng.setConfig(&cfg).announce()
}